#
# Cookbook Name:: om
# Recipe:: build_wars
#
# Copyright 2013, IBM
#
# All rights reserved - Do Not Redistribute
#


wasuser = node[:was][:user]
wasgroup = node[:was][:group]
wasuserpw = node[:was][:userpw]
cuser = node[:chef][:user]
cgroup = node[:chef][:group]
deployment = node[:deployment]

class Chef::Recipe
include Orchestrate
end

zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"
chef_gem 'zk'


startinstr "Build OM Liberty deliverables..."

# TODO: commenting sbc since its part of authoring ear.
# warfiles=["smcfs", "sma", "sbc" ]
warfiles=["smcfs", "sma"]
warfiles.each() do| warfile|
	bash "Build #{warfile}.war " do
		user wasuser
		group wasgroup
		cwd "#{node[:om][:installDir]}"
		code <<-EOH
			#{node[:om][:installDir]}/bin/buildwar.sh -Drestservice=true -Dappserver=websphere -Ddevmode=true -Dwufdevmode=false -Dwarfiles=#{warfile}
			chmod -R 755 #{node[:om][:installParentDir]}
    	EOH
	end
	bash "Upload #{warfile}.war " do
		user cuser
		group cgroup
		cwd "#{node[:om][:installDir]}"
		code <<-EOH
        	ssh -l #{node[:common][:scpuser]} #{node[:common][:scphost]} "mkdir -p /repo/generated/#{deployment}"
        	scp #{node[:om][:installDir]}/external_deployments/#{warfile}.war #{node[:common][:scp_repo]}/generated/#{deployment}/#{warfile}.war
    	EOH
	end
	ruby_block "Publishing #{warfile}.war build completion status to ZooKeeper." do
 		block do
			i_url = "/deployments/" + deployment + "/config/om/runtime/application/war/#{warfile}/status"
			dep = Dependency.new(i_url, node[:client][:status], zkep)
			dep.publish!
		end
	end
end

bash "Build properties jar for use with warfiles" do
	user wasuser
	group wasgroup
	cwd "#{node[:om][:installDir]}/properties"
	code <<-EOH
		zip -r properties.jar .
		chmod 755 properties.jar	
	EOH
end

bash "Upload properties.jar" do
	user cuser
	group cgroup
	cwd "#{node[:om][:installDir]}/properties"
	code <<-EOH
        	ssh -l #{node[:common][:scpuser]} #{node[:common][:scphost]} "mkdir -p /repo/generated/#{deployment}"
        	scp #{node[:om][:installDir]}/properties/properties.jar #{node[:common][:scp_repo]}/generated/#{deployment}/
	EOH
end

ruby_block "Publishing properties.jar build completion status to ZooKeeper." do
	block do
		i_url = "/deployments/" + deployment + "/config/om/runtime/application/properties.jar/status"
		dep = Dependency.new(i_url, node[:client][:status], zkep)
		dep.publish!
	end
end

stopinstr "Build OM Liberty deliverables..."



